﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lastfm.Services;
using System.Threading;
namespace LastFmDemo
{
    public partial class MainForm : Form
    {
        List<Track> lt;
        static String API_KEY = "d10c3c47d071f9208bb1500ac5ae3e6c";
        static String SECRET = "1225dd072c6b2c5da0079b17edc10385";
        public Session session;
        List<Track> tracks;
        List<Album> albums;
        List<Artist> artists;
        LogInForm lgf;
        public MainForm()
        {
            InitializeComponent();
        }
        public MainForm(LogInForm lgf)
        {
            InitializeComponent();
            this.lgf = lgf;
            this.AcceptButton = btSearch;
        }        

        private void MainForm_Load(object sender, EventArgs e)
        {
            cbSearch.Items.Add("Track name");        
            cbSearch.Items.Add("Album name");
            cbSearch.Items.Add("Artist");
            cbSearch.SelectedIndex = 0;
            session = new Session(API_KEY, SECRET);
            labelUsername.Text = "Hello " + lgf.User.Name;
        }
        int index = 0;
        private void btSearch_Click(object sender, EventArgs e)
        {
            index = cbSearch.SelectedIndex;            
            if (index != -1 && tbSearch.Text != "")
            {                
                switch (index)
                {
                    case 0:                        
                        tracks = Helper.SearchTrack(tbSearch.Text,session);                        
                        break;
                    case 1:                        
                        albums = Helper.SearchAlbum(tbSearch.Text, session);
                        break;
                    case 2:
                        artists = Helper.SearchArtist(tbSearch.Text,session);
                        break;
                    default:
                        break;
                }
                UpdateListItem();
            }
        }
        private void UpdateListItem()
        {            
            panelListItem.Controls.Clear();
            panelDetail.Controls.Clear();
            switch (index)
            {
                case 0:    //Tracks                
                    for (int i = 0; i < tracks.Count; i++)
                    {
                        TrackItem item = new TrackItem(tracks[i].Title.ToString(), tracks[i].Artist.ToString(),i, this);
                        
                        panelListItem.Controls.Add(item);                       
                        panelListItem.Controls[i].Location = new Point(0, 70 * i + 10);
                    }
                    if (tracks.Count == 0)
                        ShowNotFound();
                    break;
                case 1:    //Albums           
                    for (int i = 0; i < albums.Count; i++)
                    {
                        AlbumItem item = new AlbumItem(albums[i].Title.ToString(), albums[i].Artist.ToString(),i, this);
                        panelListItem.Controls.Add(item);
                        panelListItem.Controls[i].Location = new Point(0, 70 * i + 10);
                    }
                    if (albums.Count == 0)
                        ShowNotFound();
                    break;
                case 2:     //Artists
                    for (int i = 0; i < artists.Count; i++)
                    {
                        String plays = "";
                        //plays += artists[i].GetPlaycount().ToString();
                        //plays += " plays";
                        ArtistItem item = new ArtistItem(artists[i].Name.ToString(), plays, this);
                        panelListItem.Controls.Add(item);
                        panelListItem.Controls[i].Location = new Point(0, 70 * i + 10);
                        if (i == 4)
                            break;
                    }
                    if (artists.Count == 0)
                        ShowNotFound();
                    break;
                default:
                    break;
            }            
        }
        public void ShowNotFound()
        {
            Label lb = new Label();
            lb.Font = new Font("Times New Roman", 12);
            lb.Text = "No item found";
            lb.Location = new Point(10, 20);
            panelListItem.Controls.Add(lb);
        }
        public void ShowDetail(int type, int idx)
        {
            panelDetail.Controls.Clear();
            switch (type)
            {
                case 0: //Tracks
                    if (idx < tracks.Count)
                    {
                        TrackDetail detail = new TrackDetail(tracks[idx].Title, tracks[idx].Artist.Name, tracks[idx].URL);
                        panelDetail.Controls.Add(detail);
                    }
                    break;
                case 1: //Albums
                    if (idx < albums.Count)
                    {
                        String plays = albums[idx].GetPlaycount().ToString();
                        plays += " plays";
                        AlbumDetail detail = new AlbumDetail(albums[idx].Title, albums[idx].Artist.Name, albums[idx].GetImageURL(),plays,albums[idx].URL);
                        panelDetail.Controls.Add(detail);
                    }
                    break;
                case 2: //Artists
                    if (idx < artists.Count)
                    {
                        String plays=artists[idx].GetPlaycount().ToString();
                        plays+=" plays";
                        ArtistDetail detail = new ArtistDetail(artists[idx].Name, plays,artists[idx].GetImageURL());
                        panelDetail.Controls.Add(detail);
                    }
                    break;
                default:
                    break;
            }
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            lgf.Close();
        }

        private void btTopTracks_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            lt=Helper.GetTopTrack(lgf.User);
            ListViewItem item;
            for (int i = 0; i < lt.Count; i++)
            {
                item = new ListViewItem();
                item.Text = lt[i].Artist.ToString();
                item.SubItems.Add(lt[i].Title);
                listView1.Items.Add(item);
            }
        }

        private void splitContainerContent_Panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void listView1_MouseUp(object sender, MouseEventArgs e)
        {

            var hit = listView1.HitTest(e.Location);
            if (hit.SubItem != null && hit.SubItem == hit.Item.SubItems[1])
            {
                var url = new Uri(lt[hit.Item.Index].URL);
                System.Diagnostics.Process.Start(url.AbsoluteUri);
            }
        }

        private void btRecentlyPlayed_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            lt = Helper.GetRecentTrack(lgf.User);
            ListViewItem item;
            for (int i = 0; i < lt.Count; i++)
            {
                item = new ListViewItem();
                item.Text = lt[i].Artist.ToString();
                item.SubItems.Add(lt[i].Title);
                listView1.Items.Add(item);
            }
        }

        private void btLoveTrack_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            lt = Helper.GetLoveTrack(lgf.User);
            ListViewItem item;
            for (int i = 0; i < lt.Count; i++)
            {
                item = new ListViewItem();
                item.Text = lt[i].Artist.ToString();
                item.SubItems.Add(lt[i].Title);
                listView1.Items.Add(item);
            }
        }
    }
}
